﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SDLL;
using SDATA;

namespace SABI
{
    public partial class frm0041 : Form
    {
        string _VoucherTypeID = "", strPL1, strPL2, strPL3, sql;
        IGB igb = new IGB(FRM.ConnectString);
        SCBX.MultiColumnComboBox _mccb;

        public frm0041()
        {
            InitializeComponent();
        }

        public frm0041(string VoucherTypeID, SCBX.MultiColumnComboBox mccb = null)
        {
            InitializeComponent();
            _VoucherTypeID = VoucherTypeID;
            _mccb = mccb;
            txtVoucherTypeID.Enabled = false;
            chklPL1.Enabled = false;
            chklPL2.Enabled = false;
            chklPL3.Enabled = false;
            txtPL1.Enabled = false;
            txtPL2.Enabled = false;
            txtPL3.Enabled = false;
            cboPL1.Enabled = false;
            cboPL2.Enabled = false;
            cboPL3.Enabled = false;
            cboOutputOrder.Enabled = false;
        }

        private void frm0041_Shown(object sender, EventArgs e)
        {
            loadDATA_Combo();
            loadDATA_Edit();
        }

        void loadDATA_Combo()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Value");
            dt.Columns.Add("Text");
            dt.Rows.Add("0", "NSSS");
            dt.Rows.Add("1", "SNSS");
            dt.Rows.Add("2", "SSNS");
            dt.Rows.Add("3", "SSSN");
            cboOutputOrder.DataSource = dt;
            cboOutputOrder.DisplayMember = "Text";
            cboOutputOrder.ValueMember = "Value";
        }
        
        void loadDATA_Edit()
        {

            sql = string.Format("Select * From tb0027 Where VoucherTypeID='{0}'", _VoucherTypeID);
            DataTable dt = FRM.GetDataTable(sql);
            string abc = dt.Rows[0]["OutputOrder"].ToString();

            if (dt.Rows.Count != 1) { Close(); return; }

            txtVoucherTypeID.Text = dt.Rows[0]["VoucherTypeID"].ToString();
            cboOutputOrder.SelectedValue = dt.Rows[0]["OutputOrder"].ToString() != "" ? dt.Rows[0]["OutputOrder"].ToString() : "";
            chklPL1.Checked = dt.Rows[0]["Enabled1"].ToString() != "" ? true : false;
            chklPL2.Checked = dt.Rows[0]["Enabled2"].ToString() != "" ? true : false;
            chklPL3.Checked = dt.Rows[0]["Enabled3"].ToString() != "" ? true : false;

            // PL1
            switch(dt.Rows[0]["S1Type"].ToString())
            {
                case "1":
                    txtPL1.Text = FRM.Month.PadLeft(2, '0');
                    cboPL1.Text = "Theo tháng";
                    break;
                case "2":
                    txtPL1.Text = FRM.Year;
                    cboPL1.Text = "Theo năm";
                    break;
                case "3":
                    txtPL1.Text = dt.Rows[0]["S1"].ToString();
                    cboPL1.Text = "Theo loại phiếu";
                    break;
                case "4":
                    txtPL1.Text = dt.Rows[0]["S1"].ToString();
                    cboPL1.Text = "Theo đơn vị";
                    break;
                case "5":
                    txtPL1.Text = dt.Rows[0]["S1"].ToString();
                    cboPL1.Text = "Theo hằng số";
                    break;
            };
            strPL1 = txtPL1.Text;

            // PL2
            switch (dt.Rows[0]["S2Type"].ToString())
            {
                case "1":
                    txtPL2.Text = FRM.Month.PadLeft(2, '0');
                    cboPL2.Text = "Theo tháng";
                    break;
                case "2":
                    txtPL2.Text = FRM.Year;
                    cboPL2.Text = "Theo năm";
                    break;
                case "3":
                    txtPL2.Text = dt.Rows[0]["S2"].ToString();
                    cboPL2.Text = "Theo loại phiếu";
                    break;
                case "4":
                    txtPL2.Text = dt.Rows[0]["S2"].ToString();
                    cboPL2.Text = "Theo đơn vị";
                    break;
                case "5":
                    txtPL2.Text = dt.Rows[0]["S2"].ToString();
                    cboPL2.Text = "Theo hằng số";
                    break;
            };
            strPL2 = txtPL2.Text;

            // PL3
            switch (dt.Rows[0]["S3Type"].ToString())
            {
                case "1":
                    txtPL3.Text = FRM.Month.PadLeft(2,'0');
                    cboPL3.Text = "Theo tháng";
                    break;
                case "2":
                    txtPL3.Text = FRM.Year;
                    cboPL3.Text = "Theo năm";
                    break;
                case "3":
                    txtPL3.Text = dt.Rows[0]["S3"].ToString();
                    cboPL3.Text = "Theo loại phiếu";
                    break;
                case "4":
                    txtPL3.Text = dt.Rows[0]["S3"].ToString();
                    cboPL3.Text = "Theo đơn vị";
                    break;
                case "5":
                    txtPL3.Text = dt.Rows[0]["S3"].ToString();
                    cboPL3.Text = "Theo hằng số";
                    break;
            };
            strPL3 = txtPL3.Text;

            igb.TableName = "tb0191";
            igb.StringKey1 = strPL1;
            igb.StringKey2 = strPL2;
            igb.StringKey3 = strPL3;
            txtSetLastKey.Value = (int)igb.GetLastKey() + 1;


        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtSetLastKey.Value < 1) { txtSetLastKey.Value = 1; }
            igb.SaveLastKey(txtSetLastKey.Value - 1);
            if (_mccb != null) { _mccb.SelectedValue = _VoucherTypeID; }
            Close();
        }
    }
}
